package com.cloud.bc.controller.ajax.center;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloud.bc.config.Config;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcAuthentications;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.model.response.AjaxResp;
import com.cloud.bc.service.BcAuthenticationsService;
import com.cloud.bc.service.BcSinglesService;
import com.cloud.bc.service.BcUserPhotosService;
import com.cloud.bc.service.BcUsersService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@AllArgsConstructor
@RestController
@RequestMapping("m/ajax/center/auth")
public class Ajax_MAuthController {

    private final BcUsersService bcUsersService;
    private final BcSinglesService bcSinglesService;
    private final BcUserPhotosService bcUserPhotosService;
    private final Config config;

    private final BcAuthenticationsService bcAuthenticationsService;

    @PostMapping("identity")
    public AjaxResp identity(HttpServletRequest request,
                             HttpServletResponse response,
                             BcAuthentications req
    ) {
        AjaxResp ajaxResp = new AjaxResp();
        req.setIdentityCreateTime(new Date());
        if (!StrUtil.isEmpty(req.getIdCardPositivePhoto()) && !StrUtil.isEmpty(req.getIdCardBackPhoto())) {
            req.setIdentityCheck(3);
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        queryWrapper.eq("user_id", userInfo.getId());
        bcSinglesService.update(new BcSingles().setIsAuth(1), queryWrapper);
        bcAuthenticationsService.update(req, queryWrapper);
        return ajaxResp;
    }

    @PostMapping("education")
    public AjaxResp education(HttpServletRequest request,
                              HttpServletResponse response,
                              BcAuthentications req
    ) {
        AjaxResp ajaxResp = new AjaxResp();
        req.setEducationCreateTime(new Date());
        req.setEducationCheck(3);
        QueryWrapper queryWrapper = new QueryWrapper();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        queryWrapper.eq("user_id", userInfo.getId());
        bcAuthenticationsService.update(req, queryWrapper);
        bcSinglesService.update(new BcSingles().setIsAuth(1), queryWrapper);
        return ajaxResp;
    }

    @PostMapping("work")
    public AjaxResp work(HttpServletRequest request,
                         HttpServletResponse response,
                         BcAuthentications req
    ) {
        AjaxResp ajaxResp = new AjaxResp();
        req.setWorkCreateTime(new Date());
        req.setWorkCheck(3);
        QueryWrapper queryWrapper = new QueryWrapper();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        queryWrapper.eq("user_id", userInfo.getId());
        bcAuthenticationsService.update(req, queryWrapper);
        bcSinglesService.update(new BcSingles().setIsAuth(1), queryWrapper);
        return ajaxResp;
    }

    @PostMapping("hause")
    public AjaxResp hause(HttpServletRequest request,
                          HttpServletResponse response,
                          BcAuthentications req
    ) {
        AjaxResp ajaxResp = new AjaxResp();
        req.setHauseCreateTime(new Date());
        req.setHauseCheck(3);
        QueryWrapper queryWrapper = new QueryWrapper();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        queryWrapper.eq("user_id", userInfo.getId());
        bcAuthenticationsService.update(req, queryWrapper);
        bcSinglesService.update(new BcSingles().setIsAuth(1), queryWrapper);
        return ajaxResp;
    }

    @PostMapping("car")
    public AjaxResp car(HttpServletRequest request,
                        HttpServletResponse response,
                        BcAuthentications req
    ) {
        AjaxResp ajaxResp = new AjaxResp();
        req.setCarCreateTime(new Date());
        req.setCarCheck(3);
        QueryWrapper queryWrapper = new QueryWrapper();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        queryWrapper.eq("user_id", userInfo.getId());
        bcAuthenticationsService.update(req, queryWrapper);
        bcSinglesService.update(new BcSingles().setIsAuth(1), queryWrapper);
        return ajaxResp;
    }
}
